package com.idealighter.game.server.db.datadb.mapper;

import com.idealighter.game.server.db.datadb.generate.domain.RoomCardScoreDomain;
import com.idealighter.game.server.db.datadb.generate.mapper.RoomCardScoreDomainMapper;
import com.idealighter.game.server.db.datadb.mapper.domain.ScoreRangeDomain;

import java.util.Date;
import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

public interface RoomCardScoreMapper extends RoomCardScoreDomainMapper {

  @Select({ "<script>", //
      "SELECT", //
      "b.id AS id", //
      "FROM", //
      "room_card_score a", //
      "LEFT JOIN", //
      "room_card_room_list b ON a.room = b.id", //
      "WHERE", //
      "a.player_id = #{playerId} and b.group_id = 0", //
      "<if test='lastCreateTime!=null'>", //
      "   AND b.create_time &lt; #{lastCreateTime}", //
      "</if>", //
      "ORDER BY b.create_time DESC ,a.id desc", //
      "LIMIT #{limit};", //
      "</script>"//
  })
  public List<Long> selectMyRoomGameRecord(@Param("playerId") long playerId,
      @Param("lastCreateTime") Date lastCreateTime, @Param("limit") int limit);


  @Select({ "<script>", //
      "SELECT", //
      "b.id AS id", //
      "FROM", //
      "room_card_score a", //
      "LEFT JOIN", //
      "room_card_room_list b ON a.room = b.id", //
      "WHERE", //
      "a.player_id = #{playerId} and b.group_id > 0", //
      "<if test='lastCreateTime!=null'>", //
      "   AND b.create_time &lt; #{lastCreateTime}", //
      "</if>", //
      "ORDER BY b.create_time DESC ,a.id desc", //
      "LIMIT #{limit};", //
      "</script>"//
  })

  public List<Long> selectMyGroupGameRecord(@Param("playerId") long playerId,
      @Param("lastCreateTime") Date lastCreateTime, @Param("limit") int limit);



  @Select({ "<script>", //
      "SELECT", //
      "b.id AS id", //
      "FROM", //
      "room_card_score a", //
      "LEFT JOIN", //
      "room_card_room_list b ON a.room = b.id", //
      "WHERE", //
      "a.player_id = #{playerId} ", //
      "<if test='lastCreateTime!=null'>", //
      "   AND b.create_time &lt; #{lastCreateTime}", //
      "</if>", //
      "ORDER BY b.create_time DESC ,a.id desc", //
      "LIMIT #{limit};", //
      "</script>"//
  })
  public List<Long> selectMyAllGameRecord(@Param("playerId") long playerId,
      @Param("lastCreateTime") Date lastCreateTime, @Param("limit") int limit);

  @Select({ "<script>", //
      "SELECT ", //
      "ifnull(sum(win),0) AS totalWin,", //
      "b.player_id AS playerId ", //
      "FROM ", //
      "(SELECT ", //
      "    id ", //
      "FROM ", //
      "    room_card_room_list ", //
      "WHERE ", //
      "    create_time &gt;= #{createTime}", //
      "        AND end_time &lt;= #{endTime}) AS a", //
      "    LEFT JOIN", //
      "room_card_score b ON a.id = b.room", //
      "<where>", //
      "b.group_id = #{groupId}", //
      "<if test='lastPlayerId!=null'>", //
      "and b.player_id &lt; #{lastPlayerId}", //
      "</if>", //
      "</where>", //
      "GROUP BY playerId", //
      "ORDER BY playerId DESC", //
      "LIMIT #{limit};", //
      "</script>"//
  })
  public List<ScoreRangeDomain> selectScoreRange(@Param("groupId") Integer groupId,
      @Param("lastPlayerId") Long lastPlayerId, @Param("createTime") Date createTime,
      @Param("endTime") Date endTime, @Param("limit") int limit);


  // @formatter:off
  @Insert({ "<script>", //
      "insert into room_card_score (game, room, ", "group_id, player_id, ",
      "super_id, player_name, ", "player_avatar, win)", "values",
      "<foreach collection='records' item='record' separator=','>",
      "(#{record.game,jdbcType=INTEGER}, #{record.room,jdbcType=BIGINT}, ",
      "#{record.groupId,jdbcType=INTEGER}, #{record.playerId,jdbcType=BIGINT}, ",
      "#{record.superId,jdbcType=BIGINT}, #{record.playerName,jdbcType=VARCHAR}, ",
      "#{record.playerAvatar,jdbcType=VARCHAR}, #{record.win,jdbcType=BIGINT})", "</foreach>", //
      "</script>" })
  // @formatter:on
  int insertBatch(@Param("records") List<RoomCardScoreDomain> records);
}
